
public class repalceTableInString {

    public String replaceSpace(StringBuffer str) {

        if (str == null)
            return "NullStr";
        int spaceNum = 0;

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == ' ') {
                spaceNum++;
            }
        }

        int oldLength = str.length();
        int newLength = oldLength + 2 * spaceNum;

        str.setLength(newLength);       

        int oldIndex = oldLength - 1;
        int newIndex = newLength - 1;

        while (oldIndex >= 0 && oldIndex < newIndex) {
            if (str.charAt(oldIndex) == ' ') {
                str.setCharAt(newIndex--, '0');
                str.setCharAt(newIndex--, '2');
                str.setCharAt(newIndex--, '%');
            } else {
                str.setCharAt(newIndex--, str.charAt(oldIndex));
            }
 
            oldIndex--;
        }

        return str.toString();
    }

    public static void main(String[] args) {
        StringBuffer str = new StringBuffer(" ");
        repalceTableInString rs = new repalceTableInString();
        String s = rs.replaceSpace(str);
        System.out.println(s);


    }
}